<template>
  <div>
      <el-card>
          <el-input placeholder="请输入名称、类型或位置查询管理单位" v-model="selectinput" class="input-with-select" clearable @clear="getMangemants()">
            <el-button slot="append" icon="el-icon-search" @click="selectMangemant()"></el-button>
          </el-input>
          <el-button type="primary" style="margin-bottom:10px; float:right" @click="addDialogVisible=true">添加管理单位</el-button>
        <el-table :data="Mangemantlist" bvenue style="width: 100%" :row-class-name="tableRowClassName" stripe border>
            <el-table-column prop="managementnumber" label="单位标识号" width="100" fixed></el-table-column>
            <el-table-column prop="managementname" label="单位名称" min-width="150"></el-table-column>
            <el-table-column prop="managementphone" label="联系电话" min-width="170"></el-table-column>
            <el-table-column prop="managementtype" label="单位类型" min-width="80"></el-table-column>
            <el-table-column prop="managementaddress" label="单位地址" width="200"> </el-table-column>
            <el-table-column prop="managementindirect" label="单位简介" width="600"> </el-table-column>
            <el-table-column label="管理场馆" min-width="110">
                <template slot-scope="scope">
                    <el-button @click="getvenue(scope.row.managementnumber)">查看</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="145px" fixed="right">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="showEditDialog(scope.row.id)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="deleteMangemant(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="this.pagenum"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="this.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </el-card>

      <!-- 添加管理单位对话框 -->
    <el-dialog
      title="添加管理单位"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="单位标识号" prop="managementnumber">
          <el-input v-model="addForm.managementnumber"></el-input>
        </el-form-item>
        <el-form-item label="单位名称" prop="managementname">
          <el-input v-model="addForm.managementname"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="managementphone">
          <el-input v-model="addForm.managementphone"></el-input>
        </el-form-item>
        <el-form-item label="单位类型" prop="managementtype">
            <el-select v-model="value1" placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="单位地址" prop="managementaddress">
          <el-input v-model="addForm.managementaddress"></el-input>
        </el-form-item>
        <el-form-item label="单位简介" prop="managementindirect">
          <el-input v-model="addForm.managementindirect"></el-input>
        </el-form-item>
      </el-form>
        <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserInfo">确 定</el-button>
      </span>
    </el-dialog>
       <!-- 编辑管理单位对话框 -->
        <el-dialog
        title="编辑管理单位"
        :visible.sync="editDialogVisible"
        width="50%" @close="editDialogClosed">
        <!-- 内容主体区 -->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
            <el-form-item label="单位标识号" prop="managementnumber">
            <el-input v-model="editForm.managementnumber"></el-input>
            </el-form-item>
            <el-form-item label="单位名称" prop="managementname">
            <el-input v-model="editForm.managementname"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="managementphone">
            <el-input v-model="editForm.managementphone"></el-input>
            </el-form-item>
            <el-form-item label="单位类型" prop="managementtype">
            <el-select v-model="value1" placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
            </el-form-item>
            <el-form-item label="单位地址" prop="managementaddress">
            <el-input v-model="editForm.managementaddress"></el-input>
            </el-form-item>
            <el-form-item label="单位简介" prop="managementindirect">
            <el-input type="textarea" v-model="editForm.managementindirect"></el-input>
            </el-form-item>
        </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>

      <!-- 管理场馆查询页面 -->
      <el-dialog title="查询管理场馆"
        :visible.sync="getDialogVisible"
        width="90%" @close="editDialogClosed">
        <!-- 内容主体区 -->
          <el-card style="margin-top:0px">
            <el-table :data="venuelist" bvenue style="width: 100%" :row-class-name="tableRowClassName" stripe border>
              <el-table-column prop="venuenumber" label="场馆编号" width="165" fixed></el-table-column>
              <el-table-column prop="venuename" label="场馆名称" min-width="190"></el-table-column>
              <el-table-column prop="venuetype" label="场馆类型" min-width="120"></el-table-column>
              <el-table-column prop="venuepnumber" label="联系电话" min-width="140"></el-table-column>
              <el-table-column prop="venueindirect" label="场馆简介" width="220"> </el-table-column>
              <el-table-column prop="venuearea" label="场馆面积(平方米)" width="80"> </el-table-column>
              <el-table-column prop="venuepeople" label="容纳人数" width="90"> </el-table-column>
              <el-table-column prop="venuefieldnumber" label="配备场地编号" width="170"> </el-table-column>
              <el-table-column prop="venuemanagement" label="隶属管理单位编号" width="140"> </el-table-column>
              <el-table-column prop="venuedtime" label="营业时间" width="100"> </el-table-column>
              <el-table-column prop="venuebtime" label="建成时间" width="100"> </el-table-column>
              <el-table-column prop="venueattention" label="注意事项" width="220"> </el-table-column>
              <el-table-column prop="venuemoney" label="租用单价/小时" width="115"> </el-table-column>
              <el-table-column prop="venueisfree" label="是否免费" width="80"> </el-table-column>
              <el-table-column prop="venueopen" label="是否对外开放" width="110"> </el-table-column>
              <el-table-column prop="venuesite" label="场馆地址" width="220"> </el-table-column>
              <el-table-column prop="state" label="场馆状态" width="auto">
                <template slot-scope="scope">
                  <el-switch v-model="scope.row.state" disabled active-value="true"></el-switch>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="handleSizeChange1"
              @current-change="handleCurrentChange1"
              :current-page="this.pagenum1"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="this.pagesize1"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total1">
            </el-pagination>
            <el-button type="primary" style="margin-bottom:10px;float:right" @click="getDialogVisible=false">关闭</el-button>
          </el-card>
      </el-dialog>
  </div>
</template>

<script>
export default {

    data () {

      return {

        options: [{
          value: '个人',
          label: '个人'
        }, {
          value: '团体',
          label: '团体'
        }, {
          value: '公司',
          label: '公司'
        }, {
          value: '学校',
          label: '学校'
        }, {
          value: '政府',
          label: '政府'
        }],

        value1:'',

          Mangemant: null,

          newstate:'',

            // 当前的页数
            pagenum:1,
            // 当前每页显示多少条数据
            pagesize:10,
            // 普通管理单位等级
            query:'',

            Mangemantlist: [],
            total: 0,


            // 当前的页数
            pagenum1:1,
            // 当前每页显示多少条数据
            pagesize1:7,
            // 普通管理单位等级
            query1:'',
            total1: 0,


            addDialogVisible:false,

            // 添加管理单位的表单数据
        addForm: {
            managementnumber: '',
            managementname: '',
            managementphone: '',
            managementtype: '',
            managementaddress: '',
            managementindirect: '',
            managementvenuenumber: ''

        },
        value:'',
        // 添加表单的验证规则对象
        addFormRules: {
            managementnumber: [
            { required: true, message: '请输入单位标识号', trigger: 'blur' },
            { min: 3, max: 50, message: '账号的长度在3~50个字符之间', trigger: 'blur' }
            ],
            managementname: [
            { required: true, message: '单位名称', trigger: 'blur' },
            { min: 1, max: 30, message: '名字的长度在1~30个字符之间', trigger: 'blur' }
            ],
            managementtype: [
            { required: true, message: '请输入单位类型', trigger: 'blur' }
            ],
            managementphone: [
            { required: true, message: '请输入联系电话', trigger: 'blur' },
            { min: 3, max: 20, message: '联系电话的长度在3~20个字符', trigger: 'blur' }
            ],
            managementaddress: [
            { required: true, message: '请输入单位地址', trigger: 'blur' },
            { min: 2, max: 50, message: '手机号的长度在2~50个字符之间', trigger: 'blur' }
            ]
        },

         // 控制修改管理单位对话框的显示与隐藏
        editDialogVisible: false,
        // 查询到管理单位信息对象
        editForm: {},
        // 修改表单的验证规则对象
        editFormRules: {
            managementnumber: [
            { required: true, message: '请输入单位标识号', trigger: 'blur' },
            { min: 3, max: 50, message: '账号的长度在3~50个字符之间', trigger: 'blur' }
            ],
            managementname: [
            { required: true, message: '单位名称', trigger: 'blur' },
            { min: 1, max: 30, message: '名字的长度在1~30个字符之间', trigger: 'blur' }
            ],
            managementtype: [
            { required: true, message: '请输入单位类型', trigger: 'blur' }
            ],
            managementphone: [
            { required: true, message: '请输入联系电话', trigger: 'blur' },
            { min: 3, max: 20, message: '联系电话的长度在3~20个字符', trigger: 'blur' }
            ],
            managementaddress: [
            { required: true, message: '请输入单位地址', trigger: 'blur' },
            { min: 2, max: 50, message: '手机号的长度在2~50个字符之间', trigger: 'blur' }
            ]
        },

        getDialogVisible:false,

        venuelist:[],

        selectinput:'',

        value2:true
        
      }
    },
    created() {
        this.getMangemants()

    },
    methods: {

        //获取普通管理单位列表?pagenum=1&pagesize=5&query=0
        getMangemants () {
            
            this.getRequest('/management/managements/?pagenum='+this.pagenum+'&pagesize='+this.pagesize+'&query='+this.query).then(resp => {
                // console.log(resp)
                this.Mangemantlist = resp.data.current_data
                this.total = resp.data.total
            })
        },

        //删除管理单位
        deleteMangemant (id) {
            this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {

            this.deleteRequest('/management/'+id).then(resp => {
                this.getMangemants()
            }) 
            }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });          
            });
            
        },
        //账号状态
        MangemantStateChange() {
            this.addForm.state=this.value
            this.editForm.state=this.value
        },

        //添加管理单位
        addUserInfo() {
            this.addForm.managementtype=this.value1
            this.$refs.addFormRef.validate(valid => {
                if (valid) {
                    this.postRequest('/management',this.addForm).then(resp => {
                        if (resp.code==201) {
                        this.getMangemants()
                        this.addDialogVisible=false
                        }
                        return false
                    })
                } else {
                    this.$message.error('请输入必要信息！')
                    return false
                }
            })
        },


        //修改普通管理单位
        showEditDialog(id) {
            this.postRequest('/management/message/'+id).then(resp => {
                this.editForm=resp.data
                this.value1=this.editForm.managementtype
                console.log(resp)
                this.editDialogVisible=true
            })
        },

        
        //确定修改普通管理单位
        editUserInfo() {
            this.editForm.managementtype=this.value1
            this.$refs.editFormRef.validate(valid => {
                if (valid) {
                    this.putRequest('/management/updata',this.editForm).then(resp => {
                        this.getMangemants()
                        this.editDialogVisible=false
                        
                    })
                } else {
                    this.$message.error('请输入必要信息！')
                    return false
                }
            })
        },

        //查看场馆通过管理单位编号
        getvenue(id) {
            
            this.getRequest('/venue/'+id).then(resp => {
              this.venuelist=resp.data
              this.total1 = resp.data.total
              this.getDialogVisible=true
              // console.log(this.venuelist.state)
            })
        },

        //根据名称、类型或位置查询管理单位
        selectMangemant() {
            if (this.selectinput.length=='0') {
              this.$message.error('请输入名称、类型或位置开始查询！')
              
            return false
            } else {
                this.getRequest('/management/managements/?pagenum='+this.pagenum+'&pagesize='+this.pagesize+'&query='+this.selectinput).then(resp => {
                this.Mangemantlist=resp.data.current_data
              })
            }
            
        },



        // 监听 pagesize 改变的事件
        handleSizeChange(newSize) {
        // console.log(newSize)
        this.pagesize = newSize
        this.getMangemants()
        },
        // 监听 页码值 改变的事件
        handleCurrentChange(newPage) {
        //   console.log(newPage)
        this.pagenum = newPage
        this.getMangemants()
        },


        // 监听 pagesize1 改变的事件
        handleSizeChange1(newSize) {
        // console.log(newSize)
        this.pagesize1 = newSize

        },
        // 监听 页码值 改变的事件
        handleCurrentChange1(newPage) {
        //   console.log(newPage)
        this.pagenum1 = newPage
        
        },

        // 监听添加管理单位对话框的关闭事件
        addDialogClosed() {
        this.$refs.addFormRef.resetFields()
        },
        // 监听修改管理单位对话框的关闭事件
        editDialogClosed() {
          this.$refs.editFormRef.resetFields()
        }
    }
}
</script>

<style lang="less" scoped>
.el-card {
    margin-top:20px;
}

.input-with-select {
    width: 40%;
}
</style>>
